package com.umeox.watch.moto.launcher.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SubscriptionManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.umeox.moto.common.utils.NetUtils;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.util.UToast;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.constant.AppEntry;


public class NoNetworkActivity extends ThemeAppCompatActivity {

    public static final String APN_SETTINGS_ACTIVITY = "com.android.settings.Settings$ApnSettingsActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.no_network_activity);
	}

	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void start(View view) {
        startActivity(new Intent(NoNetworkActivity.this, MainActivity.class));
        finish();
    }

    public void gotoWifiSetting(View view) {
        Intent intent = new Intent();
        intent.setClassName(AppEntry.Settings.PKG, AppEntry.Settings.CLS);
        startActivity(intent);
    }

    public void chooseApn(View view) {
        if (NetUtils.hasSimCard()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                int subId = getSubId();
                if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
                    Intent apnIntent = new Intent();
                    apnIntent.setClassName(AppEntry.ApnSettings.PKG, APN_SETTINGS_ACTIVITY);
                    apnIntent.putExtra("sub_id", subId);
                    startActivity(apnIntent);
                }
            } else {
                Intent intent = new Intent();
                intent.setClassName(AppEntry.ApnSettings.PKG, AppEntry.ApnSettings.CLS);
                startActivity(intent);
            }
        }else {
            UToast.showCustomToast(this, getString(R.string.no_simcard_str), Toast.LENGTH_LONG);
        }

    }

    public void retry(View view) {
        Intent intent = new Intent(NoNetworkActivity.this,DetectSignalActivity.class);
        startActivity(intent);
    }


    private int getSubId() {
        int subid = -1;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            subid = SubscriptionManager.getDefaultDataSubscriptionId();
        }
        return subid;
    }
}
